package com.example.springbootexamples;

import ch.qos.logback.core.status.Status;
import org.apache.commons.collections.MapUtils;
import org.apache.tomcat.util.net.AbstractEndpoint;
import org.junit.Test;
import org.springframework.util.AntPathMatcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TreeExample {


    List<Map<String,Object>> list(){
        return null;
    }

    Map<String,Object> getMap(String step,String type){

        Map<String,Object> map=new HashMap<>();
        if(type==null){
            type="http";
        }
        map.put("step",step);
        map.put("type",type);
        AbstractEndpoint abstractEndpoint;
        return map;
    }


    @Test
    public void t(){
        AntPathMatcher antPathMatcher;

        List<Map<String,Object>> list=new ArrayList<>();
        list.add(getMap("1","http"));
        list.add(getMap("1,1","log"));
        list.add(getMap("1,2","log"));
        list.add(getMap("1,3","log"));
        list.add(getMap("1,4","http"));
        list.add(getMap("1,5","sql"));
        list.add(getMap("1,3,1","http"));
        list.add(getMap("1,4,1","log"));
        list.add(getMap("1,4,1,1","http"));

        getTree(list);

        Map<String,Object> root=getRoot(list);


        traverse(root);

        for(Map<String,Object> map:list){
            List<Map<String,Object>> child=getchild(map);
            map.put("child",child);
        }

        System.out.println("only http===============");
        traverse(root);

    }

    Map<String,Object> getRoot(List<Map<String,Object>> list){
        for(Map<String,Object> map:list){
            String step=MapUtils.getString(map,"step");
            if("1".equals(step)){
                return map;
            }
        }
        return null;
    }

    void traverse(Map<String,Object> root){
        if(root!=null){
            System.out.println(root);
            List<Map<String,Object>> child= (List<Map<String, Object>>) root.get("child");
            if(child!=null){
                for(Map<String,Object> map:child){
                    traverse(map);
                }
            }

        }
    }

    /*void travere(List<Map<String,Object>> list){

        for(Map<String,Object> map:list){

        }

    }*/

    void getTree(List<Map<String,Object>> list){

        for(Map<String,Object> m1:list){
                    String step=MapUtils.getString(m1,"step");
            for(Map<String,Object> m2:list){
                if(m1!=m2){
                    String step1=MapUtils.getString(m2,"step");

                    if(step.startsWith(step1)){
                        String v=step.substring(step1.length()+1);
                        if(v.matches("^\\d+$")){
                            List<Map<String,Object>> child= (List<Map<String, Object>>) m2.get("child");
                            if(child==null){
                                child=new ArrayList<>();
                            }
                            child.add(m1);
                            m2.put("child",child);
                        }
                    }

                }
            }
        }



    }


    List<Map<String,Object>> getchild(Map<String,Object> node) {
        List<Map<String, Object>> child = (List<Map<String, Object>>)
                node.get("child");
        if (child != null) {
            List<Map<String, Object>> childList = new ArrayList<>();
            for (Map<String, Object> c : child) {
                String type = MapUtils.getString(c, "type");
                if ("http".equals(type)) {
                    childList.add(c);
                } else {
                    List<Map<String, Object>> cc = getchild(c);
                    if (cc != null)
                        childList.addAll(cc);
                }
            }
            return childList;
        }
        return null;

    }
}
